package com.clwl.supervise.superviseapp.service.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.clwl.supervise.superviseapp.entity.base.SysDictData;
import com.clwl.supervise.superviseapp.entity.scan.TjFoodTypeTree;
import com.clwl.supervise.superviseapp.entity.scan.TjFoodtype;
import com.clwl.supervise.superviseapp.entity.scan.TjTjjbz;
import com.clwl.supervise.superviseapp.mapper.base.SysDictDataMapper;
import com.clwl.supervise.superviseapp.mapper.scan.TjFoodtypeMapper;
import com.clwl.supervise.superviseapp.mapper.scan.TjTjjbzMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ScanInfoService {


    @Resource
    private TjFoodtypeMapper tjFoodtypeMapper;

    @Resource
    private TjTjjbzMapper tjTjjbzMapper;

    @Resource
    private SysDictDataMapper sysDictDataMapper;


    /**
     * 获取扫描原料使用的原料列表
     * @return
     */
    public List<TjFoodTypeTree> getMaterialsTypeList(String type) {
        List<TjFoodTypeTree> resultList = new ArrayList<>();
        QueryWrapper<TjFoodtype>   queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("TYPE",type);
        //查询所有数据
        List<TjFoodtype> foodtypeList = tjFoodtypeMapper.selectList(queryWrapper);
        //将所有数据存储到list中
        for (TjFoodtype tjFoodtype : foodtypeList) {
            resultList.add(new TjFoodTypeTree(tjFoodtype.getId(), tjFoodtype.getParentId(), tjFoodtype.getName(),tjFoodtype.getCode()));
        }
        List<TjFoodTypeTree> tree = TjFoodTypeTree.buildTree(resultList);
        return tree;

    }
    /**
     * 获取扫描原料使用的原料列表
     * @return
     */
    public List<TjFoodtype> getMaterialsTypeListByParent(String parentId) {
        QueryWrapper<TjFoodtype> queryWrapper = new QueryWrapper<>();
        if(parentId == null) {
            queryWrapper.isNull("parent_id");
        } else {
            queryWrapper.eq("parent_id", parentId);
        }
        List<TjFoodtype> foodtypeList = tjFoodtypeMapper.selectList(queryWrapper);
        for (TjFoodtype tjFoodtype : foodtypeList) {
            tjFoodtype.setValue(tjFoodtype.getId());
            tjFoodtype.setText(tjFoodtype.getName());
        }
        return foodtypeList;

    }
    /**
     * 通过选择的原料类型和识别完成的原料信息进行识别
     * @param classFoodType  分类code
     * @param rawMaterialFoodList 原料识别信息
     * @return
     */
    public Map<String, Object> getMaterialsScanResult(String classFoodType, String rawMaterialFoodList) {
        //通过一级分类,二级分类，三级分类查询出所有的添加剂信息
        QueryWrapper queryWrapper =   new QueryWrapper<>();
        queryWrapper.eq("FT_CODE",classFoodType);
        //查询此选择的类型下能查询到的添加剂信息
        List<TjTjjbz> tjjbzList = tjTjjbzMapper.selectList(queryWrapper);

        //原料识别信息
        String[] rawMaterialFoodArray = rawMaterialFoodList.split(",");
        List<String> rawMaterialsList = Arrays.asList(rawMaterialFoodArray);

        //筛选不是合法添加剂的,如果根据类型查询出来的添加剂和识别的添加剂匹配上的就是合法的添加剂，如果没有匹配上的就不是合法添加剂
        List<TjTjjbz>  qualifiedAdditivesList = new ArrayList<>();
        for (String rawMaterials : rawMaterialsList) {
            for (TjTjjbz tjTjjbz : tjjbzList) {
                if (tjTjjbz.getName().equals(rawMaterials)) {
                    qualifiedAdditivesList.add(tjTjjbz);
                }
            }
        }
        List<String>  nonLegalAdditivesList = rawMaterialsList.stream().filter( rawMaterials -> {
            for (TjTjjbz tjTjjbz : tjjbzList) {
                if (tjTjjbz.getName().equals(rawMaterials)) {
                    return false;
                }
            }
            return true;
        }).collect(Collectors.toList());
        //剩余不是合格的添加剂在2760表中查看是否存在，如果不存在则是非添加剂，如果存在则是非法添加剂（因为上面已经匹配了这个类型该有的添加剂了）
        QueryWrapper  allAdditivesQ = new QueryWrapper<>();
        allAdditivesQ.in("NAME",nonLegalAdditivesList);
        //非法添加剂
        List<TjTjjbz> illegalAdditivesList = tjTjjbzMapper.selectList(allAdditivesQ);
        //非添加剂, 不在上面查询出来的非法添加剂里面的剩余的就是非添加剂了
        List<String> nonAdditiveList = nonLegalAdditivesList.stream().filter(rawMaterials -> {
            for (TjTjjbz tjTjjbz : illegalAdditivesList) {
                if (tjTjjbz.getName().equals(rawMaterials)) {
                    return false;
                }
            }
            return true;
        }).collect(Collectors.toList());
        Map<String, Object> resultMap = new HashMap<>();
        //合法添加剂
        resultMap.put("qualifiedAdditivesList",qualifiedAdditivesList);
        //非法添加剂
        resultMap.put("illegalAdditivesList",illegalAdditivesList);
        //非添加剂
        resultMap.put("nonAdditiveList",nonAdditiveList);
        return resultMap;
    }

    /**
     * 获取2760判定标准
     * @return
     */
    public List<SysDictData> get2760TypeList() {
        return sysDictDataMapper.selectByDictTypeCode("sys_tjjgf");
    }
}
